package service;

import java.io.IOException;
import java.util.List;
import java.util.Observable;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.faces.bean.ApplicationScoped;
import javax.inject.Named;

import ejb.PositionEJBRemote;
import ejb.PositionStatusEJBRemote;
import entityJPA.Candidate;
import entityJPA.Position;
import entityJPA.Positionstatus;
import exportPDF.Export;

/**
 * Session Bean implementation class PositionService
 */
@Stateless
@LocalBean
public class PositionService extends Observable {
	@EJB
	PositionEJBRemote positionEJB;
	@EJB
	Export ex;
	@EJB
	CandidateService candidateService;
	@EJB PositionStatusEJBRemote positionStatus;

	private static Logger logger = Logger
			.getLogger(UserService.class.getName());

	/**
	 * Default constructor.
	 */
	public PositionService() {
		// TODO Auto-generated constructor stub
		FileHandler fh;
		try {
			fh = new FileHandler("C:/PositionLoggerFile.log", true);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			logger.addHandler(fh);
			// logger.log(Level.SEVERE, "Error doing XYZ");
			// logger.info("My first log");
		} catch (SecurityException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void insert(Position p) throws Exception {
		try {
			positionEJB.persistPosition(p);
			logger.info("inserted position:" + p.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}
		setChanged();
		notifyObservers("add");

	}

	public void update(Position p) throws Exception {
		try {
			if (getPositionById(p.getIdPosition()).getPositionstatus()
					.getValue().equals("active")){
				positionEJB.mergePosition(p);
				logger.info("updated position:" + p.toString());
			}
			else
				throw new Exception();
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}
		setChanged();
		notifyObservers("update");
	}

	public void activate(Position p) throws Exception {
		try {
			positionEJB.mergePosition(p);
			logger.info("updated position:" + p.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}
		setChanged();
		notifyObservers("update");
	}

	public void remove(Position p) throws Exception {
		try {
			positionEJB.mergePosition(p);
			logger.info("remove position:" + p.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}
		setChanged();
		notifyObservers("remove");
	}

	public List<Position> getAllPositions() throws Exception {
		return positionEJB.getPositionFindAll();
	}

	public Position findPositionByName(String name) {
		try {
			for (Position p : getAllPositions()) {
				if (p.getName().equals(name))
					return p;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public Position getPositionById(int idPosition) {
		try {
			for (Position p : getAllPositions())
				if (p.getIdPosition() == idPosition)
					return p;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public Vector<Position> getActivePositions() throws Exception {
		List<Position> all = getAllPositions();
		Vector<Position> active = new Vector<Position>();
		for (Position p : all) {
			if (p.getPositionstatus().getValue().equalsIgnoreCase("active")) {
				active.add(p);
			}
		}
		return active;
	}

	public Vector<Position> getInactivePositions() throws Exception {
		List<Position> all = getAllPositions();
		Vector<Position> inactive = new Vector<Position>();
		for (Position p : all) {
			if (p.getPositionstatus().getValue().equalsIgnoreCase("inactive")) {
				inactive.add(p);
			}
		}
		return inactive;
	}

	public void decrementNumberOfPersons(Position p) throws Exception {
		try {
			positionEJB.decrementNumberOfPersons(p);
			logger.info("decremented number of persons for position:"
					+ p.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
			throw e;
		}
	}

	public byte[] exportPosition(int idPosition) {
		// Export ex=new Export();
		return ex.exportPosition(getPositionById(idPosition));
	}

	public byte[] exportPositionAndCandidates(int idPosition) {
		Position p = getPositionById(idPosition);
		Vector<Candidate> candList = candidateService
				.getCandidatesForPosition(p);
		return ex.exportPositionAndCandidates(p, candList);
	}
	
	public List<Position> getPositionPageActive(int startIndex, int endIndex){
		return positionEJB.getPositionPageActive(startIndex, endIndex);
	}
	
	public List<Position> getPositionPageInactive(int startIndex, int endIndex){
		return positionEJB.getPositionPageInactive(startIndex, endIndex);
	}
	
	public int getTotalActive(){
		return positionEJB.getTotalActive();
	}
	public int getTotalInactive(){
		return positionEJB.getTotalInactive();
	}
	
	public Positionstatus getPositionSatusByValue(String v){
		return positionStatus.getPositionStatusByValue(v);
	}
}
